#include "Enemy.h"
#include "../Graphic/GraphicManager.h"
#include "../Game/Level.h"


//Update the enemy position
void cEnemy::Update(float lfTimeStep)
{
  //To control the speed of the enemy he can only be moved every 075 ms
  mfElapsedTime += lfTimeStep;
  if (mfElapsedTime < 0.125) return;
  
  //Local variables that hold the next position
  unsigned int luiX = muiX;
  unsigned int luiY = muiY;
  
  //Get the player
  cObject * loPlayer = cLevel::Get().GetPlayer();
  
  float lfDistance = loPlayer->GetDistance(luiX, luiY);
  eOrientation leOrientation = loPlayer->GetOrientation(luiX, luiY);

  //If the enemy is far to hear the player goes out
  if (lfDistance > 10) return;

  //Try to move there
  switch (leOrientation)
  {
    case eO_North:
      luiY++;
      break;
    case eO_NorthEast:
      luiY++;
      luiX--;
      break;
    case eO_East:
      luiX--;
      break;
    case eO_SouthEast:
      luiY--;
      luiX--;
      break;
    case eO_South:
      luiY--;
      break;
   case eO_SouthWest:
      luiY--;
      luiX++;
      break;
    case eO_West:
      luiX++;
      break;
    case eO_NorthWest:
      luiY++;
      luiX++;
      break; 
    default:
      break;
  }

  //Check walls
  cLevel &lLevel = cLevel::Get();
  if (!lLevel.IsWall( luiX, luiY ))
  {
    muiX = luiX;
    muiY = luiY;
    mfElapsedTime = 0;
  }
}

//Render the enemy
void cEnemy::Render()
{
  cGraphicManager::Get().SetColor(eBlack, eLightBlue);
  cGraphicManager::Get().WriteChar(muiX, muiY, mcSymbol);
}
